<?php
/* ******************************
 *	Recipie Production List aJax actions
 * ******************************
 */
 
include('../../../includes/classes/core.php');
$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {

    case 'get_edit_page':
	    $prod_id	= $_REQUEST['id'];
		$page		= GetPage(GetProduction($prod_id));
		
		$data		= array('page'	=> $page);

        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    
	    $start		= $_REQUEST['start'];
	    $end		= $_REQUEST['end'];
	    $client		= $_REQUEST['client_id'];
	    
	    $rResult = mysql_query("SELECT      	`a_letter`.`id`,
	    										CONCAT(' ა - ', `a_letter`.`id`),
												`client`.`name`,
												SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) AS sum,
												ROUND((SUM(`sale_transaction_detail`.`product_quantity` * `sale_transaction_detail`.`self_cost`)), 2) AS self,
												ROUND((SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) - SUM(sale_transaction_detail.product_quantity * sale_transaction_detail.self_cost)), 2) AS diff
									FROM        `client`
									LEFT JOIN   `client_objects` ON `client`.`id` = `client_objects`.`client_id`
									LEFT JOIN   `a_letter` ON `client_objects`.`id` = `a_letter`.`client_object_id`
									LEFT JOIN   `warehouse` ON `a_letter`.`id` = `warehouse`.`a_letter_id`
									LEFT JOIN   `warehouse_detail` ON `warehouse`.`id` = `warehouse_detail`.`warehouse_id`
									LEFT JOIN   `warehouse_producing` ON `warehouse_detail`.`id` = `warehouse_producing`.`warehouse_detail_id`
									LEFT  JOIN  `sale_transaction_detail` ON `warehouse_producing`.`id` = `sale_transaction_detail`.`warehouse_producing_id`
									RIGHT JOIN  `cartridge_pricelist` ON `client`.`id` = `cartridge_pricelist`.`client_id` AND `warehouse_producing`.`cartridge_part_id` = `cartridge_pricelist`.`parts_type` AND `cartridge_pricelist`.`production_id` = `warehouse_detail`.`prod_id`
								WHERE     	DATE(a_letter.finish_date)  BETWEEN date('$start') AND date('$end') AND client.id = $client
								GROUP BY  	a_letter.id
");
	    
		$data = array(
			"aaData"	=> array()
		);

		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;

    case 'get_letter':
       $letter_id	= $_REQUEST['id'];
       $page		= getLetter($letter_id);
        
       $data		= array('page'	=> $page);
        
       break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>		    
	    	<legend>ძირითადი ინფორმაცია</legend>
    		<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="trans_production">პროდუქტი</label></td>
					<td>
						<div class="seoy-row" id="production_name_seoy">
							<input type="text" id="trans_production" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button class="combobox">trans_production</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="trans_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="trans_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="production_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}


function getLetter($letter_id){
	global $sum;
	global $res;
	$rsql = mysql_query("SELECT     warehouse_detail.barcode,
							        production.`name`,
							        CASE WHEN ISNULL(part2.id) THEN 0
							         WHEN NOT ISNULL(part2.id) THEN 1
							        END AS 'ტონერი',
							        CASE WHEN ISNULL(part3.id) THEN 0
							         WHEN NOT ISNULL(part3.id) THEN 1
							        END AS 'ბარაბანი',
							        CASE WHEN ISNULL(part4.id) THEN 0
							         WHEN NOT ISNULL(part4.id) THEN 1
							        END AS 'მაგნიტური ლილვი',
							        CASE WHEN ISNULL(part5.id) THEN 0
							         WHEN NOT ISNULL(part5.id) THEN 1
							        END AS 'რეზინის ლილვი',
							        CASE WHEN ISNULL(part6.id) THEN 0
							         WHEN NOT ISNULL(part6.id) THEN 1
							        END AS 'გამწ. რაკელი',
							        CASE WHEN ISNULL(part7.id) THEN 0
							         WHEN NOT ISNULL(part7.id) THEN 1
							        END AS 'დოზ. რაკელი',
							        CASE WHEN ISNULL(part8.id) THEN 0
							         WHEN NOT ISNULL(part8.id) THEN 1
							        END AS 'მიკრო ჩიპი',
							        CASE WHEN ISNULL(part9.id) THEN 0
							         WHEN NOT ISNULL(part9.id) THEN 1
							        END AS 'კორპუსი',
							        IF(`warehouse_detail`.`type` = 2, 1, 0) AS 'ტიპი',
							        SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) as `cartridge_price`,
									ROUND(SUM(sale_transaction_detail.product_quantity * sale_transaction_detail.self_cost), 2) AS self,
									ROUND((SUM(IF(`warehouse_detail`.`type` = 2, 0, `cartridge_pricelist`.`price`)) - SUM(sale_transaction_detail.product_quantity * sale_transaction_detail.self_cost)), 2)AS diff,
									CONCAT(`client`.`name`, '(', `client_objects`.`name`, ')') AS `client_objects`,
									`a_letter`.`finish_date` AS `finish_date`
				       FROM    warehouse_producing
				       LEFT JOIN  warehouse_detail ON warehouse_detail.id = warehouse_producing.warehouse_detail_id
				       LEFT JOIN  warehouse ON warehouse.id = warehouse_detail.warehouse_id
				       LEFT JOIN  a_letter ON a_letter.id = warehouse.a_letter_id
				       LEFT JOIN  production ON production.id = warehouse_detail.prod_id
				       LEFT JOIN  client_objects ON client_objects.id = a_letter.client_object_id
				       LEFT JOIN  client ON client.id = client_objects.client_id
				       LEFT JOIN  cartridge_pricelist ON client.id = cartridge_pricelist.client_id AND warehouse_producing.cartridge_part_id = cartridge_pricelist.parts_type AND warehouse_detail.prod_id  = cartridge_pricelist.production_id
				       LEFT JOIN  warehouse_producing AS part2 ON part2.cartridge_part_id = 2 AND part2.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part3 ON part3.cartridge_part_id = 3 AND part3.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part4 ON part4.cartridge_part_id = 4 AND part4.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part5 ON part5.cartridge_part_id = 5 AND part5.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part6 ON part6.cartridge_part_id = 6 AND part6.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part7 ON part7.cartridge_part_id = 7 AND part7.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part8 ON part8.cartridge_part_id = 8 AND part8.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part9 ON part8.cartridge_part_id = 9 AND part9.warehouse_detail_id = warehouse_detail.id
				       LEFT JOIN  warehouse_producing AS part1 ON part2.cartridge_part_id = 1 AND part1.warehouse_detail_id = warehouse_detail.id
					   LEFT  JOIN   sale_transaction_detail ON warehouse_producing.id = sale_transaction_detail.warehouse_producing_id
				       WHERE	  a_letter.id = '" . $letter_id ."'
					   GROUP BY warehouse_detail.id");
	
	$result = mysql_fetch_assoc( mysql_query("	SELECT		CONCAT(`client`.`name`, '(', `client_objects`.`name`, ')') AS `client_objects`,
															`a_letter`.`finish_date` AS `finish_date`
												FROM    	a_letter
												LEFT JOIN	client_objects ON client_objects.id = a_letter.client_object_id
												LEFT JOIN	client ON client.id = client_objects.client_id
												WHERE		a_letter.id = '" . $letter_id ."'"));
												


	$data = '
	<html>
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
			<title>ბ ბარათი</title>
			<style type="text/css">
			
			
			
			.job-blank{
				width: 668px;
				border-style:solid !important;
				border-width:1px  !important;			
			}
			
			.job-blank td{
				text-align: center;
				font-weight: normal;
				font-size: 10.0pt;
				border-style:solid !important;
				border-width:1px  !important;
			}
			
			.job-blank .rotate{
				font-size: 8pt;
				writing-mode: tb-rl;
				-webkit-transform: rotate(270deg);
				-moz-transform: rotate(270deg);
				-o-transform: rotate(270deg);
				white-space: nowrap;
				display: block;
				width: 8px;
				vertical-align: bottom;
			}
			
			.style1{
				margin-top: 40px;
				margin-left: 4px;
				vertical-align: bottom;
			}
			
			.style2{
				margin-top: 75px;
				margin-left: 4px;
				vertical-align: bottom;
			}
			
			
			.sub{
				width: 25px;
			}
			</style>
		</head>
	
	<body >
			<fieldset style=" padding: 15px 15px 15px 15px; border: solid 1px #A6C9E2; background-color: #F1F7FD; background-image: url(\'media/images/forms/bg.png\' ); ">
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center">
						<th colspan="2">
							<label for="client_object">მიღება ჩაბარების აქტი №: ბ – </label>'.$letter_id.'</br>
							<label for="client_object">თარიღი  :&nbsp;&nbsp;&nbsp;&nbsp;</label>'.$result['finish_date'].'</br>
							<label for="client_object">მომსახურების ობიექტი  :&nbsp;&nbsp;&nbsp;&nbsp;</label>'.$result['client_objects'].'
						</th>
					</tr>
				</table>
			</fieldset>
									
		<div >
									
	<fieldset style=" padding: 5px 1px 5px 1px; border: solid 1px #A6C9E2; background-color: #F1F7FD; background-image: url(\'media/images/forms/bg.png\' ); ">
		<legend style="margin-left: 15px;">შესრულებული სამუშაოები</legend>									
		<table border="1" class="job-blank" cellpadding="0" cellspacing="0">
			<tr style="background-color: #D9D9D9; font-size: 9pt;font-weight: normal;  height: 30px;">
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td colspan="9" style=" font-size: 11pt;font-weight: normal; vertical-align:middle">ჩატარებული სამუშაოები</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td>&nbsp;</td>																		
			</tr>
			<tr>
				<td style="width: 150px;font-size: 9pt;"><span>შტრიხკოდი</span></td>
				<td style="width: 170px;font-size: 9pt;"><span>მოდელი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style1">ტონერი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style1">ბარაბანი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style2">მაგნიტ. ლილ.</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style2">რეზინის PCR</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style2">გამწ. რაკელი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style2">დოზ. რაკელი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style1">ჩიპი</span></td>
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style1">კორპუსი</span></td>	
				<td class="sub" style="vertical-align: bottom !important;"><span class="rotate style2">საგარანტიო</span></td>
				<td style="font-size: 9pt;width: 50px;">&nbsp;ფასი&nbsp;</td>	
				<td style="font-size: 9pt;width: 50px;">თვითღირ.</td>
				<td style="font-size: 9pt;width: 50px;">სხვაობა</td>																			
			</tr>';

			while ( $aRow = mysql_fetch_array( $rsql ) ){
				$data .= '<tr>';
				for ( $i = 0 ; $i < 14 ; $i++ ){
					if($i< 2 || $i > 10){
							$data .='<td>'.$aRow[$i].'</td>';
					}else{
						if($aRow[$i] == 1){
							$data .= '<td><img src="media/images/0.gif"></td>';
						}else{
							$data .= '<td>&nbsp;</td>';
						}
					}
					 if($i == 11){
						$sum = floatval($sum) + floatval($aRow[$i]);
					}
					if($i == 12){
						$sum1 = floatval($sum1) + floatval($aRow[$i]);
					}
					if($i == 13){
						$sum2 = floatval($sum2) + floatval($aRow[$i]);
					} 
				}
				$data .= '</tr>';
		}
		$data .='
				<tr>
					<td colspan="11" style="text-align: right;">სულ:&nbsp;</td>
					<td>'.$sum.'</td>
					<td>'.$sum1.'</td>
					<td>'.$sum2.'</td>
				</tr>
					</table>
				</fieldset>';
		return $data;
	}

?>